package com.example.demo33.api;

import com.example.demo33.entity.InviteCode;
import com.example.demo33.service.InviteCodeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/api/invite-codes")
public class InviteCodeController {

    private final InviteCodeService inviteCodeService;

    public InviteCodeController(InviteCodeService inviteCodeService) {
        this.inviteCodeService = inviteCodeService;
    }

    @PostMapping
    public ResponseEntity<?> generateInviteCode(
            @RequestParam String password,
            @RequestParam Long creatorId
    ) {
        try {
            InviteCode inviteCode = inviteCodeService.generateInviteCode(password, creatorId);
            return ResponseEntity.ok(inviteCode);
        } catch (SecurityException e) {
            // 方案1：使用Collections.singletonMap
            Map<String, String> errorMap = Collections.singletonMap("error", "口令验证失败");
            return ResponseEntity.status(401).body(errorMap);

        }
    }
}